#include "chooselevelscene.h"
#include <QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{

    //选择关卡音效
    QSound *chooseSound =new QSound(":/res/music/TapButtonSound.wav",this);
    //返回按钮音效
    QSound *backSound =new QSound(":/res/music/BackButtonSound.wav",this);

    //设置主页面图片尺寸
    this->setFixedSize(640,1176);
    //设置标题
    this->setWindowTitle("Make our Choice!");
    //设置图标
    this->setWindowIcon(QPixmap(":/res/image/Coin0001.png"));
    //创建菜单栏
    QMenuBar *bar=menuBar();
    this->setMenuBar(bar);
    //创建菜单
    QMenu * startMenu =bar->addMenu("go!(ง •_•)ง");
    //创建菜单项
    QAction *quitAction =startMenu->addAction("quit(┭┮﹏┭┮)");
    //监听退出
            connect(quitAction,&QAction::triggered,[=](){
        this->close();

    });

            //返回按钮
            MyPushButton * backBtn =new MyPushButton(":/res/image/BackButton.png",":/res/image/BackButtonSelected.png");

            backBtn->setParent(this);
            backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

            connect(backBtn,&MyPushButton::clicked,[=](){
                //播放音效
               backSound->play();
                //qDebug()<<"返回到主场景";
                QTimer::singleShot(500,this,[=]{

                     //延时 发送点击返回按钮的信号
                    emit this->ChooseSceneBack();
                });

            });
            pScene =NULL;
           //创建20个具体关卡
            for(int i=0;i<20;i++){
                MyPushButton * menuBtn=new MyPushButton(":/res/image/LevelIcon.png");
                menuBtn->setParent(this);
                menuBtn->move(50+(i%4)*150,100+(i/4)*200);
                connect(menuBtn,&MyPushButton::clicked,[=](){

                    //播放音效
                    chooseSound->play();
                   // qDebug()<<"您选择的是第"<<i+1<<"关";
                    this->pScene=new PlayScene(i+1);
                    this->pScene->setGeometry(this->geometry());
                    this->pScene->show();
                    this->hide();

                    //监听第三个场景返回按钮信号
                    connect(this->pScene,&PlayScene::ChooseSceneBack,[=](){
                        this->setGeometry(this->geometry());
                        this->show();
                        this->pScene->hide();
                        delete this->pScene;
                        this->pScene=NULL;

                    });
                });


               //现实数字
                QLabel *label =new QLabel;
                label->setParent(this);
                label->move(50+(i%4)*150,100+(i/4)*200);
                //设置尺寸
                label->setFixedSize(menuBtn->width(),menuBtn->height());
                //设置文本
                label->setText(QString::number(i+1));
                //设置对齐方式
                label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
               //设置属性，鼠标穿透属性
                label->setAttribute(Qt::WA_TransparentForMouseEvents);

            }

}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/image/OtherSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题资源
    pix.load(":/res/image/Title.png");
    //缩放图片
    //pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    //绘制标题
    painter.drawPixmap(0,0,pix);
}
